<?php

namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Db;
use think\Request;


class Words extends Base
{
    /**
     * yang yang
     * 留言页
     * @throws
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	public function words(Request $request){
		if($request->isAjax()){
   		$uid=session('uid');
   		$no=time();
   		$ra=Db::name('rewords')->where('room',$uid)->find();
   		if($ra){
   		    $room="";
   		    $bz=$ra['no'];
        }else{
   		    $room=$uid;
   		    $bz=$no.rand(10000,99999);
        }
    	$content=$request->param('words');
    	$users=Db::name('users')->where('id',$uid)->find();

            $res=Db::name('rewords')->insertGetId([
                'user_id'=>$uid,
                'name'=>$users['nickname'],
                'avatar'=>$users['photo'],
                'content'=>$content,
                'type'=>1,
                'is_teacher'=>0,
                'time'=>$no,
                'room'=>$room,
                'sort'=>1,
                'no'=>$bz
            ]);
    	if($res){
    		$this->success('留言成功');
    	}else{
    		$this->error('留言失败');
    	}

   	}   
		return $this->fetch();
	}


    /**
     * yang yang
     * 留言列表
     * @return mixed
     */
   public function list_words(){
	
    return $this->fetch();
   }

    /**
     * yang yang
     * 留言列表滚动加载数据
     * @throws
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function loaddata(Request $request)
    {
        if ($request->isAjax()) {
            $uid=session('uid');
            $identify=Db::name('users')->where('id',$uid)->field('identity')->find();
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 5;
            if($identify['identity']==1){
              $res=Db::name('rewords')->where('is_teacher',0)->where('room','>',0)->order('order_time', 'desc')->page($page, $limit)->select();
                foreach ($res as $index => $re) {

                    $res[$index]['time'] = date('Y-m-d H:i:s',$re['time']);
                    //判断有无用户回复
                    $sort=Db::name('rewords')->where('no',$re['no'])->where('is_teacher',0)->where('sort',1)->select();
                    if($sort){
                        $huifu=1;
                        $res[$index]['huifu']=$huifu;
                    }
                    $content=Db::name('rewords')->where('no',$re['no'])->where('is_teacher',0)->order('time', 'desc')->select();
                    $res[$index]['new_content']=$content[0]['content'];
                    $res[$index]['new_avatar']=$content[0]['avatar'];
                    $res[$index]['new_type']=$content[0]['type'];
                    $res[$index]['new_time']=date('Y-m-d H:i:s',$content[0]['time']);
                }


                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            }
            if($identify['identity']==0){
                $res=Db::name('rewords')->where('is_teacher',0)->where('room','=',$uid)->order('time', 'desc')->page($page, $limit)->select();

                foreach ($res as $index => $re) {

                    $res[$index]['time'] = date('Y-m-d H:i:s',$re['time']);
                    //判断有无讲师回复
                    $sort=Db::name('rewords')->where('no',$re['no'])->where('is_teacher',1)->where('sort',1)->select();
                    if($sort){
                        $huifu=1;
                        $res[$index]['huifu']=$huifu;
                    }
                    $content=Db::name('rewords')->where('no',$re['no'])->where('is_teacher',1)->order('time', 'desc')->select();
                    $res[$index]['new_content']=$content[0]['content'];
                    $res[$index]['new_avatar']=$content[0]['avatar'];
                    $res[$index]['new_type']=$content[0]['type'];
                    $res[$index]['new_time']=date('Y-m-d H:i:s',$content[0]['time']);
                }


                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            }


        }
    }

    /**
     * yang yang
     * 留言详情
     * @throws
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function details(Request $request){
        $uid=session('uid');
        $identify=Db::name('users')->where('id',$uid)->field('identity')->find();
        if($identify['identity']==1){
            //获取id值
            $id = $request->param('id');
            if ($request->isGet()) {

                /*$weixin = new Weixin();
                $url = request()->domain() . $this->request->url();
                $js_config = $weixin->js_config($url);
                $this->assign('jsconfig', $js_config);*/

                if (!$id) {
                    $this->error('id 异常');
                }
                $this->assign('id', $id);
                return $this->fetch();
            }
        }
        if($identify['identity']==0){
            $res=Db::name('rewords')->where('room',$uid)->field('no')->find();
            if($res){
                $sort=Db::name('rewords')->where('no',$res['no'])->select();
                foreach ($sort as $index=>$re){
                    if($re['sort']==1&&$re['is_teacher']==1){
                        Db::name('rewords')->where('id',$re['id'])->update(['sort'=>0]);
                    }
                }
                $content=Db::name('rewords')->where('no',$res['no'])->order('time', 'desc')->select();
                $id=$content[0]['id'];
            }else{
                $id="";
            }
            $this->assign('id', $id);
            return $this->fetch();
        }

    }

    /**
     * yang yang
     * 聊天界面数据渲染
     * @throws
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function list_details(Request $request){
	    if($request->isAjax()){
            $uid=session('uid');
            $identify=Db::name('users')->where('id',$uid)->field('identity')->find();
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 3;
            if($identify['identity']==1){
                $id = $request->param('id');

                $res=Db::name('rewords')->where('id',$id)->find();
                $rr=Db::name('rewords')->where('no',$res['no'])->order('time','desc')->page($page,$limit)->select();

                krsort($rr);

                if($rr){
                    foreach($rr as $index=>$d){

                        $d['time'] = date('Y-m-d H:i:s',$d['time']);
                        $newRr[] = $d;
                    }

                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($newRr), 'data' => $newRr]);

                }
            }
            if($identify['identity']==0){
                $res=Db::name('rewords')->where('room',$uid)->find();
                $rr=Db::name('rewords')->where('no',$res['no'])->order('time','desc')->page($page,$limit)->select();

                krsort($rr);

                if($rr){
                    foreach($rr as $index=>$d){

                        $d['time'] = date('Y-m-d H:i:s',$d['time']);
                        $newRr[] = $d;
                    }

                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($newRr), 'data' => $newRr]);

                }
            }


        }
    }


    /**
     * yang yang
     * 发送文字
     * @throws
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function words_replay(Request $request){
        if($request->isAjax()){
            $uid=session('uid');
            $users=Db::name('users')->where('id',$uid)->find();
            //获取id值
            $id = $request->param('id');
            //获取回复内容
            $repcont = $request->param('wordscont');
            $user_id=Db::name('rewords')->where('id',$id)->field('user_id,no')->find();
            if($users['identity']==0){
                $no=time();
                $ra=Db::name('rewords')->where('room',$uid)->find();
                if($ra){
                    $room="";
                    $bz=$ra['no'];
                    $reply_uid=$user_id['user_id'];
                    Db::name('rewords')->where('room',$ra['room'])->update(['order_time'=>$no]);
                }else{
                    $room=$uid;
                    $bz=$no.rand(10000,99999);
                    $reply_uid="";
                }

                $res=Db::name('rewords')->insertGetId([
                    'user_id'=>$uid,
                    'name'=>$users['nickname'],
                    'avatar'=>$users['photo'],
                    'content'=>$repcont,
                    'type'=>1,
                    'is_teacher'=>0,
                    'time'=>$no,
                    'order_time'=>$no,
                    'room'=>$room,
                    'sort'=>1,
                    'no'=>$bz,
                    'reply_uid'=>$reply_uid,
                    'words_id'=>$id,
                ]);
                if($res){
                    $this->success('发送成功');
                }else{
                    $this->error('发送失败');
                }
            }
            if($users['identity']==1){
                $inse=Db::name('rewords')->insertGetId([
                    'user_id'=>$uid,
                    'name'=>$users['nickname'],
                    'avatar'=>$users['photo'],
                    'content'=>$repcont,
                    'type'=>1,
                    'is_teacher'=>1,
                    'reply_uid'=>$user_id['user_id'],
                    'time'=>time(),
                    'words_id'=>$id,
                    'sort'=>1,
                    'no'=>$user_id['no']
                ]);

                if($inse){
                    $this->success('发送成功');
                }else{
                    $this->error('发送失败');
                }
            }
        }
    }

    /**
     * yang yang
     * 发送语音
     * @throws
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function voice(Request $request){
	   if($request->isAjax()){

           $uid=session('uid');
           $users=Db::name('users')->where('id',$uid)->find();
           //获取id值
           $id = $request->param('id');
           $user_id=Db::name('rewords')->where('id',$id)->field('user_id,no')->find();
           //音频
           $voice=$request->param('voice');
           //音频时长
           $audio_time=$request->param('audio_time');
           //处理音频
           $first=substr($voice,9);
           $last=substr($first,0,strlen($first)-4);
           if($users['identity']==0){
               $no=time();
               $ra=Db::name('rewords')->where('room',$uid)->find();
               if($ra){
                   $room="";
                   $bz=$ra['no'];
                   $reply_uid=$user_id['user_id'];
                   Db::name('rewords')->where('room',$ra['room'])->update(['order_time'=>$no]);
               }else{
                   $room=$uid;
                   $bz=$no.rand(10000,99999);
                   $reply_uid="";
               }

               $res=Db::name('rewords')->insertGetId([
                   'user_id'=>$uid,
                   'name'=>$users['nickname'],
                   'avatar'=>$users['photo'],
                   'content'=>$voice,
                   'type'=>3,
                   'is_teacher'=>0,
                   'time'=>$no,
                   'order_time'=>$no,
                   'room'=>$room,
                   'sort'=>1,
                   'no'=>$bz,
                   'reply_uid'=>$reply_uid,
                   'words_id'=>$id,
                   'audio'=>$last,
                   'audio_time'=>$audio_time
               ]);
               if($res){
                   $this->success('发送成功');
               }else{
                   $this->error('发送失败');
               }
           }
           if($users['identity']==1){
               $inse=Db::name('rewords')->insertGetId([
                   'user_id'=>$uid,
                   'name'=>$users['nickname'],
                   'avatar'=>$users['photo'],
                   'content'=>$voice,
                   'type'=>3,
                   'is_teacher'=>1,
                   'reply_uid'=>$user_id['user_id'],
                   'time'=>time(),
                   'words_id'=>$id,
                   'no'=>$user_id['no'],
                   'audio'=>$last,
                   'sort'=>1,
                   'audio_time'=>$audio_time
               ]);
               if($inse){
                   $this->success('发送成功');
               }else{
                   $this->error('发送失败');
               }
           }

       }
    }

    /**
     * yang yang
     * 发送图片
     * @throws
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function picture(Request $request){
	    if($request->isAjax()){
            $uid=session('uid');
            //图片
            $thumb = $request->param('thumb');
            //获取id值
            $id = $request->param('id');
            $user_id=Db::name('rewords')->where('id',$id)->field('user_id,no')->find();
            $users=Db::name('users')->where('id',$uid)->find();

            if($users['identity']==0){
                $no=time();
                $ra=Db::name('rewords')->where('room',$uid)->find();
                if($ra){
                    $room="";
                    $bz=$ra['no'];
                    $reply_uid=$user_id['user_id'];
                    Db::name('rewords')->where('room',$ra['room'])->update(['order_time'=>$no]);
                }else{
                    $room=$uid;
                    $bz=$no.rand(10000,99999);
                    $reply_uid="";
                }

                $res=Db::name('rewords')->insertGetId([
                    'user_id'=>$uid,
                    'name'=>$users['nickname'],
                    'avatar'=>$users['photo'],
                    'content'=>$thumb,
                    'type'=>2,
                    'is_teacher'=>0,
                    'time'=>$no,
                    'order_time'=>$no,
                    'room'=>$room,
                    'sort'=>1,
                    'no'=>$bz,
                    'reply_uid'=>$reply_uid,
                    'words_id'=>$id,
                ]);
                if($res){
                    $this->success('发送成功');
                }else{
                    $this->error('发送失败');
                }
            }
            if($users['identity']==1){
                $inse=Db::name('rewords')->insertGetId([
                    'user_id'=>$uid,
                    'name'=>$users['nickname'],
                    'avatar'=>$users['photo'],
                    'content'=>$thumb,
                    'type'=>2,
                    'is_teacher'=>1,
                    'reply_uid'=>$user_id['user_id'],
                    'time'=>time(),
                    'words_id'=>$id,
                    'sort'=>1,
                    'no'=>$user_id['no']
                ]);

                if($inse){
                    $this->success('发送成功');
                }else{
                    $this->error('发送失败');
                }
            }
        }
    }

    /**
     * yang yang
     * 改变未读回复状态
     * @throws
     * @param Request $request
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function sort(Request $request){
	    if($request->isAjax()){
            $uid=session('uid');
            //获取id值
            $id = $request->param('id');
            $res=Db::name('rewords')->where('id',$id)->find();
            $sort=Db::name('rewords')->where('no',$res['no'])->select();
            //判断身份
            $is_teacher=Db::name('users')->where('id',$uid)->field('identity')->find();

            if($is_teacher['identity']==1){
                foreach ($sort as $index=>$re){
                    if($re['sort']==1&&$re['is_teacher']==0){
                        Db::name('rewords')->where('id',$re['id'])->update(['sort'=>0]);
                    }
                }
            }
            if($is_teacher['identity']==0){
                foreach ($sort as $index=>$re){
                    if($re['sort']==1&&$re['is_teacher']==1){
                        Db::name('rewords')->where('id',$re['id'])->update(['sort'=>0]);
                    }
                }
            }

        }
    }
}
